<template>
  <div>
    <Modal :mask-closable="false" title="修改密码" v-model="isDialog" @on-cancel="onClose">
      <Form :model="form" label-position="left" :label-width="100" style="padding:0 30px;">
        <FormItem label="用户名：">
          <Input v-model="name" disabled />
        </FormItem>
        <FormItem label="原密码：">
          <input type="password" style="display: none;" />
          <Input
            v-model="form.formerPassword"
            type="password"
            autocomplete="new-password"
            password
          />
        </FormItem>
        <FormItem label="新密码：">
          <Input v-model="form.passWord" type="password" password />
        </FormItem>
        <FormItem label="确认新密码：">
          <Input v-model="form.passWordAgain" type="password" password />
        </FormItem>
      </Form>
      <div slot="footer">
        <Button @click="onClose">{{$t('common.Cancle')}}</Button>
        <Button type="primary" @click="onSubmit">{{$t('maintenance.Confirmed')}}</Button>
      </div>
    </Modal>
  </div>
</template>
<script>
export default {
  data() {
    return {
      isDialog: false,
      form: {},
      name: '',
    }
  },
  mounted() {
    this.name = window.localStorage.getItem('ziyunFactoryPortalName')
  },
  methods: {
    onClose() {
      this.form = {}
      this.isDialog = false
    },
    onSubmit() {
      if (
        this.form.formerPassword &&
        this.form.passWord &&
        this.form.passWordAgain
      ) {
        if (this.form.passWord !== this.form.passWordAgain) {
          this.$Message.error(({content:"两次密码不一样，请修改！",duration:6,closable:true}));
          return
        }
        this.axios({
          method:'get',
          url: "/basic/user/updatePwd",
          params:{
            newPwd:this.form.passWord,
            oldPwd:this.form.formerPassword
          }
        }).then(res=>{
            if(res.code===200){
                this.$Message.success('密码修改成功，请重新登陆')
                this.isDialog = false
                window.localStorage.removeItem('ziyunFactoryPortalToken')
                this.$router.push({ path: '/' })
            }else{
                this.$Message.warning(({content:res.msg,duration:6,closable:true}));
            }
        });
      } else {
        this.$Message.error(({content:"请填写完整",duration:6,closable:true}));
      }
    },
  },
}
</script>
